/**
 * Created by fanfz on 2016/8/8 0008.
 */
$(function () {
   $("#add_address_btn").click(function () {
       var receiver=$("#receiver").val();
       var site=$("#site").val();
       var email=$("#email").val();
       var postcode=$("#postcode").val();
       var phone=$("#phone").val();
       var build=$("#build").val();

       if (receiver == '' || receiver.length < 2 || receiver.length > 20) {
           alert('警告：收货人不得为空并且不得小于两位并且不得大于20位！');
           $("#receiver").focus(); //将焦点以至表单字段
           return false;
       }
       else if (site == ''  ) {
           alert('警告：收获地址不得为空！');
           $("#site").focus();//将焦点以至表单字段
           return false;
       }
       else if (!/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/.test(email)) {
           alert('邮件格式不正确');
           $("#email").focus(); //将焦点以至表单字段
           return false;
       }
       else if (postcode == '' || postcode.length > 6 ) {
           alert('警告：邮政编码不得为空并且不得大于六位！');
           $("#postcode").focus();//将焦点以至表单字段
           return false;
       }
       else if (phone == '' || phone.length > 11 ||(!/^1[3|5|8]\d{9}$/.test(phone))) {
           alert('警告：请填写正确的手机号码！');
           $("#phone").focus(); //将焦点以至表单字段
           return false;
       }
       else if (build== ''  ) {
           alert('警告：标志建筑不得为空！');
           $("#build").focus();//将焦点以至表单字段
           return false;
       }
       else
       {
           $.ajax({
               url:"index.php?a=add_address",
               type:"post",
               data:{receiver:receiver,phone:phone,build:build,postcode:postcode,email:email,site:site},
               dataType:'json',
               success:function (data) {
                   if(data.status==1)
                   {
                       $('#addressModal').modal('hide');
                       var str="<tr><td><label><input class='sid' type='radio' name='sid' value='"+data.insert_id+"' checked></label></td><td>"+receiver+"</td><td>"+site+"</td><td>"+phone+"</td><td>"+email+"</td><td>"+postcode+"</td><td>"+build+"</td></tr>";
                       $("#address_table").append(str);
                   }
                   else
                   {
                       alert(data.msg);
                   }
               }
           });
       }
   });
    $("#go_calculate").click(function () {
        var sid=$(".sid:checked").val();
        var oid=$('#oid').val();
        $.ajax({
            url:"index.php?a=go_pay",
            data:{sid:sid,oid:oid},
            type:'post',
            dataType:'json',
            success:function (data) {
                if(data.status==1)
                {
                    alert(data.msg);
                    window.location.href=data.url;
                }
                else
                {
                    alert(data.msg);
                }
            }
        })
    });
});
